#!/bin/sh

PGOOD_TIMEOUT=2

#GPIO_NUM = GPIOAA1 = GPIO_BASE + GPIOAA + 1 = 280 + 26 * 8 + 1 = 489
# GPIO_BASE=$(cat /sys/class/gpio/gpiochip*/base)
GPIO_NUM=491
devmem 0x32b30088 32 0x75
echo $GPIO_NUM > /sys/class/gpio/export
echo lower > /sys/class/gpio/gpio${GPIO_NUM}/direction
GPIO_PATH=/sys/class/gpio/gpio$GPIO_NUM

old_pgood=2
init_flag=1
#busctl set-property  xyz.openbmc_project.State.BMC /xyz/openbmc_project/state/bmc0 xyz.openbmc_project.State.BMC CurrentBMCState s xyz.openbmc_project.State.BMC.BMCState.Ready

while true
do
        sleep $PGOOD_TIMEOUT
		pgood=`cat $GPIO_PATH/value`

        #host state changed
        if [ $pgood -ne $old_pgood ]
        then
            if [ $pgood -eq 1 ]
            then
                #sel_report_log
                if [ ! -f "/run/initramfs/power_reboot_flag" ];then
                    if [ $init_flag -eq 1 ];then
                        let init_flag=0
                    else
                        sel_report_log POWER_ON
                    fi
                else
                    rm /run/initramfs/power_reboot_flag
                fi
                
                #chassis policy flag
                touch /etc/power_btn_on
                
                #modify web power status
                echo "change host state to running"
                busctl set-property xyz.openbmc_project.State.Host /xyz/openbmc_project/state/host0 xyz.openbmc_project.State.Host CurrentHostState s  xyz.openbmc_project.State.Host.HostState.Running
                busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState s xyz.openbmc_project.State.Chassis.PowerState.On
                busctl emit /org/openbmc/control/power0 org.openbmc.control.Power PowerGood
                #解决开机机后OCP卡不通
                #/usr/sbin/host_start_ncsi.sh &
            elif [ $pgood -eq 0 ]
            then
                #sel_report_log
                if [ ! -f "/run/initramfs/power_reboot_flag" ];then
                    if [ $init_flag -eq 1 ];then
                        let init_flag=0
                    else
                        sel_report_log POWER_OFF
                    fi
                fi
                #解决关机日志在熔断恢复日志之前的问题
                if [ -f "/tmp/ThermalTrip_produce" ];then
                    rm /tmp/ThermalTrip_produce
                    touch /tmp/ThermalTrip_restore
                fi

                #chassis policy flag
                rm /etc/power_btn_on

                #modify web power status
                echo "change host state to poweroff"
                busctl set-property xyz.openbmc_project.State.Host /xyz/openbmc_project/state/host0 xyz.openbmc_project.State.Host CurrentHostState s  xyz.openbmc_project.State.Host.HostState.Off
                busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState s xyz.openbmc_project.State.Chassis.PowerState.Off
                busctl emit /org/openbmc/control/power0 org.openbmc.control.Power PowerLost

                #clean pci/cpu/dimm dbus interface
               # echo "clean_fruid: pci cpu dimm"
               # /usr/sbin/clean_fruid_bios.sh &
               # touch /etc/clean_dimm_error_flag
               # touch /etc/dimm_ras_error_flag

                #解决关机后ncsi不通
                #/usr/sbin/host_start_ncsi.sh &

                #检测NTP服务状态，如果连接远程服务器失败，则卸载nbd设备
                #check_nbd_link_status &
            else
                echo "unknown host status"
            fi
            old_pgood=$pgood
        fi
done

